package cn.com.utt.unms.mapper;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import cn.com.utt.unms.pojo.Page;
import cn.com.utt.unms.pojo.PageList;

@Mapper
public interface PageListMapper {

	@Insert("insert into `page_list`(user_id,name,title) values(#{userId},#{name},#{title})")
	@Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
	void save(PageList pageList);

	@Update("update `page_list` set name=#{name},title=#{title} where id=#{id}")
	void updateOrder(PageList pageList);
	
	@Select({"<script>",
	    "SELECT COUNT(*) AS total", 
	    "FROM page_list",
	    "WHERE user_id=#{userId} ",
	      "<if test='searchs != null'>",
	      "<foreach item='item' index='index' collection='searchs'",
	        "open='and' separator='and' close=''>",
	        "<choose>",
	        "<when test='item.op != \"like\"'>",
	        "${item.column}",
	        " = ",
	        "#{item.value}",
	        "</when>",
	        "<otherwise>",
	        "${item.column}",
	        " like ",
	        "'%${item.value}%'",
	        "</otherwise>",
	        "</choose>",
	      "</foreach>",
	      "</if>",
	    "</script>"})
	int countByCondition(Map<String, Object> conditions);
	
	@Select({"<script>",
	    "SELECT *", 
	    "FROM page_list",
	    "WHERE user_id=#{userId} ",
	      "<if test='searchs != null'>",
	      "<foreach item='item' index='index' collection='searchs'",
	        "open='and' separator='and' close=''>",
	        "<choose>",
	        "<when test='item.op != \"like\"'>",
	        "${item.column}",
	        " = ",
	        "#{item.value}",
	        "</when>",
	        "<otherwise>",
	        "${item.column}",
	        " like ",
	        "'%${item.value}%'",
	        "</otherwise>",
	        "</choose>",
	      "</foreach>",
	      "</if>",
	      "<if test='orderColumn != null'>",
	      "order by ${orderColumn} ${orderDirection}",
	      "</if>",
	      "limit #{start},#{rows}",
	    "</script>"})
	List<PageList> queryByCondition(Map<String, Object> conditions);
	
	@Select("select * from page where pageList_id=#{plId}")
	List<Page> queryPages(@Param("plId") Integer plId);
	
	@Delete({"<script>",
	    "delete ", 
	    "FROM page_list", 
	    "WHERE",
	    "id IN", 
	      "<foreach item='item' index='index' collection='ids'",
	        "open='(' separator=',' close=')'>",
	        "#{item}",
	      "</foreach>",
	"</script>"})
	void del(@Param("ids") List<Long> ids);
	
	@Select("select * from page_list where user_id=#{userId}")
	List<PageList> queryAll(@Param("userId") Long userId);
	
	@Select("select * from page_list where id=#{id}")
	PageList queryById(@Param("id") Long id);

}